package com.flyee.utils.load.contorl
{
	import com.flyee.utils.load.model.LoaderModel;
	import com.flyee.utils.load.processor.LoadLevelType;
	import com.flyee.utils.load.processor.LoadType;
	/**
	 *管理加载数组 
	 * @author hwust
	 * 
	 */	
	public class LoaderArrayManager
	{
		private var LoaderArray1:Array = null;
		private var LoaderArray2:Array = null;
		private var LoaderArray3:Array = null;
		
		private var _loadingArray:Array = null;
		private var _loadedArray:Array = null;
		private var badLoadedArray:Array = null;
		public function LoaderArrayManager()
		{
			LoaderArray1 = new Array();
			LoaderArray2 = new Array();
			LoaderArray3 = new Array();
			_loadedArray = new Array();
			_loadingArray = new Array();
			badLoadedArray = new Array();
		}
		public function addLoader(loader:LoaderModel):void
		{
			var tempArray:Array = null;
			if(loader.loadLevel == LoadLevelType.LV_1)
			{
				tempArray = LoaderArray1;
			}
			else if(loader.loadLevel == LoadLevelType.LV_2)
			{
				tempArray = LoaderArray2;
			}
			else if(loader.loadLevel == LoadLevelType.LV_3)
			{
				tempArray = LoaderArray3;
			}
			else
			{
				//加载错误
				return
			}
			tempArray.push(loader);
		}
		public function getBlankLoader():LoaderModel
		{
			if(LoaderArray3.length > 0)
			{
				return LoaderArray3.shift();
			}
			else if(LoaderArray2.length > 0)
			{
				return LoaderArray2.shift();
			}
			else if(LoaderArray1.length > 0)
			{
				return LoaderArray1.shift();
			}
			return null;
		}
		public function get loadingArray():Array
		{
			return this._loadingArray;
		}
		public function addloadedArray(loader:LoaderModel):void
		{
			//this._loadedArray.push(loader);
		}
		public function addLoading(loader:LoaderModel):void
		{
			this._loadingArray.push(loader);
		}
		public function addBadLoad(loader:LoaderModel):Boolean
		{
			var tempString:String = loader.loadURL;
			var tempLength:uint = this.badLoadedArray.length;
			for(var i:int = 0; i< tempLength;i++)
			{
				if(this.badLoadedArray[i] == tempString)
				{
					return false
				}
			}
			this.badLoadedArray.push(tempString);
			return true;
		}
		
		public function removeWaiteLoader(id:uint):LoaderModel
		{
			var tempLoader:LoaderModel = null;
			tempLoader = this.compareWithID(id,this.LoaderArray1);
			if(tempLoader == null)
			{
				tempLoader = this.compareWithID(id,this.LoaderArray2);
			}
			if(tempLoader == null)
			{
				tempLoader = this.compareWithID(id,this.LoaderArray3);
			}
			return tempLoader
		}
		public function removeLoadingLoader(id:uint):LoaderModel
		{
			var tempLoader:LoaderModel = this.compareWithID(id,this._loadingArray);
			if(tempLoader != null)
			{
				tempLoader.needClose = true;
				
			}
			return tempLoader;
		}
		public function removeLoadedLoader(id:uint):LoaderModel
		{
			var tempLoader:LoaderModel = this.compareWithID(id,this._loadedArray);
			return tempLoader;
		}
		private function compareWithID(id:uint,tempArray:Array,isDelete:Boolean = true):LoaderModel
		{
			var tempLength:uint = tempArray.length;
			for(var i:int = 0;i< tempLength;i++)
			{
				if(tempArray[i].loadID == id)
				{
					var tempLoader:LoaderModel = tempArray[i];
					if(isDelete)
					{
						tempArray.splice(i,1);
					}
					return tempLoader;
				}
			}
			return null;
		}
		private function compareWithURL(url:String,tempArray:Array,isDelete:Boolean = true):LoaderModel
		{
			var tempLength:uint = tempArray.length;
			for(var i:int = 0;i< tempLength;i++)
			{
				if(tempArray[i].loadURL == url)
				{
					var tempLoader:LoaderModel = tempArray[i];
					if(isDelete)
					{
						tempArray.splice(i,1);
					}
					return tempLoader;
				}
			}
			return null;
		}
		public function removeLoader(id:uint):LoaderModel
		{
			var tempLoader:LoaderModel = this.removeWaiteLoader(id);
			if(tempLoader == null)
			{
				tempLoader = this.removeLoadingLoader(id);
			}
			if(tempLoader == null)
			{
				tempLoader = this.removeLoadedLoader(id);
			}
			return tempLoader;
		}
		
		public function clear():void
		{
			var tempLength:uint = 0;

			LoaderArray1.splice(0);
			LoaderArray2.splice(0);
			LoaderArray3.splice(0);
			
			tempLength = this._loadingArray.length;
			if(tempLength > 0)
			{
				
				for(var i:int = 0; i< tempLength; i++)
				{
					var tempLoader:LoaderModel = this._loadingArray[i];
					if(tempLoader.loadType != LoadType.LOADING_LOAD)
					{
						tempLoader.needClose = true;
					}
				}
				this._loadingArray.splice(0);
			}
			
			tempLength = this._loadedArray.length;
			if(tempLength > 0)
			{
				for(var k:int = 0; k<tempLength ; k++)
				{
					var tempLoader4:LoaderModel = this._loadedArray[k];
					//tempLoader4.unloadAndStop(true);
					
					tempLoader4.unload()
				}
				this._loadedArray.splice(0);
			}
		}
	}
}